﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DiscountSharing.LocalModels;
using Microsoft.Phone.Shell;

namespace DiscountSharing
{
    public partial class Discounts : PhoneApplicationPage
    {
        public Discounts()
        {
            InitializeComponent();
            InitAppBar();
        }

        private void InitAppBar()
        {
            ApplicationBarIconButton composeButton = new ApplicationBarIconButton(new Uri("/Images/MenuIcon/appbar.edit.rest.png", UriKind.Relative));
            composeButton.Text = "compose";
            composeButton.Click += (o, e) => 
                {
                    NavigationService.Navigate(new Uri("/Compose.xaml", UriKind.Relative));
                };
            ApplicationBar.Buttons.Add(composeButton);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
            {
                DiscountStream.QueryContext = NavigationContext.QueryString;
            }
            
            //update title
            if (NavigationContext.QueryString.ContainsKey("show"))
            {
                switch (NavigationContext.QueryString["show"])
                {
                    case "all":
                        PageTitle.Text = ((App)App.Current).Me.Name;
                        FriendProfile.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    case "by_type":
                        FriendProfile.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    case "by_friend":
                        foreach (Friend f in ((App)App.Current).Friends)
                        {
                            if (f.ID == NavigationContext.QueryString["id"])
                            {
                                PageTitle.Text = f.Name;
                                FriendProfile.CurrentFriend = f;
                            }
                        }
                        FriendProfile.Visibility = System.Windows.Visibility.Visible;
                        break;
                    default:
                        break;
                }
            }
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);

            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
            {
                NavigationService.RemoveBackEntry();
            }
        }
    }
}